package com.learn.project2.service.admin.comment;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.MarketComment;
import com.learn.project2.bean.MarketCommentExample;
import com.learn.project2.mapper.MarketCommentMapper;
import com.learn.project2.model.bo.admin.comment.CommentDeleteBO;
import com.learn.project2.model.bo.admin.comment.CommentListBO;
import com.learn.project2.model.vo.admin.comment.CommentListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yaml.snakeyaml.events.Event;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author 岳子昂
 * @date 2022-01-07 15:45
 * @version 1.0
 */

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    MarketCommentMapper commentMapper;


    /**
     * @description:
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-07 20:50
     */
    @Override
    public CommentListVO commentList(CommentListBO commentListBO) {
        Integer page = commentListBO.getPage();
        Integer limit = commentListBO.getLimit();
        String order = commentListBO.getOrder();
        String sort = commentListBO.getSort();
        Integer userId = commentListBO.getUserId();
        Integer valueId = commentListBO.getValueId();

        // 开启分页工具
        PageHelper.startPage(page,limit);

        // 运用逆向工程获取整个list
        MarketCommentExample example = new MarketCommentExample();
        example.setOrderByClause(sort+" "+order);
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null){
            criteria.andValueIdEqualTo(valueId);
        }

        // 获取整体list
        List<MarketComment> commentList = commentMapper.selectByExample(example);
        // 获取装到vo里的list
        List<CommentListVO.ListDTO> list = new ArrayList<>();
        for (MarketComment item : commentList) {
            CommentListVO.ListDTO listBean = new CommentListVO.ListDTO();
            listBean.setId(item.getId());
            listBean.setValueId(item.getValueId());
            if (item.getType() != null){
                listBean.setType(((int) item.getType()));
            }
            listBean.setContent(item.getContent());
            listBean.setAdminContent(item.getAdminContent());
            listBean.setUserId(item.getUserId());
            listBean.setHasPicture(item.getHasPicture());
            listBean.setPicUrls(item.getPicUrls());
            if (item.getStar() != null){
                listBean.setStar(((int) item.getStar()));
            }
            // 将pojo中日期转换为vo中要求的字符串
            if (item.getAddTime() != null){
                listBean.setAddTime(item.getAddTime().toString());
            }
            if (item.getUpdateTime() != null){
                listBean.setUpdateTime(item.getUpdateTime().toString());
            }
            listBean.setDeleted(item.getDeleted());

            // 将最终处理好的listBean加入到vo中的list里
//            if (!listBean.getDeleted()) {
            list.add(listBean);
//            }
        }

        // 获取pageinfo来获取页面信息
        PageInfo<MarketComment> pageInfo = new PageInfo<MarketComment>(commentList);
        Integer total = (int) pageInfo.getTotal();
        int pages = pageInfo.getPages();

        CommentListVO commentListVO = new CommentListVO();
        commentListVO.setTotal(total);
        commentListVO.setPages(pages);
        commentListVO.setLimit(limit);
        commentListVO.setPage(page);
        commentListVO.setList(list);

        return commentListVO;
    }

    /**
     * @description:
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-07 20:50
     */
    @Override
    @Transactional
    public Integer commentDelete(CommentDeleteBO commentDeleteBO) {
        Integer id = commentDeleteBO.getId();
//        int affectedRows = commentMapper.deleteByPrimaryKey(id);

        // TODO：以下为逻辑删除代码，有时间再改
        MarketComment marketComment = new MarketComment();
        marketComment.setId(id);
        marketComment.setUpdateTime(new Date());
        marketComment.setDeleted(true);
        int affectedRows = commentMapper.updateByPrimaryKeySelective(marketComment);
        return affectedRows;
    }
}
